import { NStep, type StepProps } from "naive-ui";
import type { HTMLAttributes } from "vue";
import { getSlotsDom } from "../utils";
import type { BaseComponentType } from "../types";

export type StepFactoryConstructorType = {
  attrs?: HTMLAttributes;
  props?: StepProps;
  defaultSlot?: BaseComponentType;
  iconSlot?: BaseComponentType;
  titleSlot?: BaseComponentType;
};

export class StepFactory {
  private attrs: HTMLAttributes = {};
  private props: StepProps = {};
  private defaultSlot: BaseComponentType = null;
  private iconSlot: BaseComponentType = null;
  private titleSlot: BaseComponentType = null;

  constructor(data?: StepFactoryConstructorType) {
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.props) this.setProps(data.props);
    if (data?.defaultSlot) this.setDefault(data.defaultSlot);
    if (data?.iconSlot) this.setIcon(data.iconSlot);
    if (data?.titleSlot) this.setTitle(data.titleSlot);
  }

  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }
  setProps(props: StepProps) {
    this.props = props;
    return this;
  }
  setDefault(defaultSlot: BaseComponentType) {
    this.defaultSlot = defaultSlot;
    return this;
  }
  setIcon(iconSlot: BaseComponentType) {
    this.iconSlot = iconSlot;
    return this;
  }

  setTitle(titleSlot: BaseComponentType) {
    this.titleSlot = titleSlot;
    return this;
  }

  create() {
    return (
      <NStep {...this.attrs} {...this.props}>
        {{
          default:
            this.defaultSlot != null
              ? () => getSlotsDom(this.defaultSlot)
              : null,
          icon: this.iconSlot != null ? () => getSlotsDom(this.iconSlot) : null,
          title:
            this.titleSlot != null ? () => getSlotsDom(this.titleSlot) : null,
        }}
      </NStep>
    );
  }
}
